import { SetResponseNormal } from "../axdr/SetResponseNormal";
import { FrameData } from "../bean/FrameData";
import { DataUtils } from "../DataUtils";
import { resultCode } from "../tools/DataTool";
import { SetResponseNormalList } from "../axdr/SetResponseNormalList";
import { SetThenGetResponseNormalList } from "../axdr/SetThenGetResponseNormalList";
import { parseData } from "./ParseTools";


export function parseSetNormalResponse(setNormalResponse: SetResponseNormal): String {
    let set_response_normal = {
        dar: {},
    };
    let frameData = new FrameData();
    setNormalResponse.result.dar.encodeAndSave(1000);
    set_response_normal.dar = DataUtils.bytesToHexString(setNormalResponse.result.dar.dataCode, 0, setNormalResponse.result.dar.dataCode.length);
    frameData.code = resultCode.success
    frameData.data = set_response_normal;
    return JSON.stringify(frameData);
}

export function parseSetNormalResponseList(setResponseNormalList: SetResponseNormalList): String {
    let set_response_list = {
        seq_of_dar: {},
    };
    let frameData = new FrameData();
    let seqOfDar = new Array<any>();
    for (let i = 0; i < setResponseNormalList.results.size(); i++) {
        let dar = {
            dar: {}
        }
        let setoadDar = setResponseNormalList.results.get(i);
        setoadDar.dar.encodeAndSave(1000);
        dar.dar = DataUtils.bytesToHexString(setoadDar.dar.dataCode, 0, setoadDar.dar.dataCode.length);
        seqOfDar.push(dar)
    }
    set_response_list.seq_of_dar = seqOfDar;
    frameData.code = resultCode.success;
    frameData.data = set_response_list;
    return JSON.stringify(frameData);
}

export function parseSetThenGetResponse(setThenGetResponse: SetThenGetResponseNormalList): String {
    let set_then_get_list = {
        seq_result: {}
    }
    let frameData = new FrameData();
    let resultArray = new Array<any>();
    for (let i = 0; i < setThenGetResponse.reads.size(); i++) {
        let result = {
            seq_dar: {},
            get_result: {}
        }
        setThenGetResponse.reads.get(i).result.dar.encodeAndSave(1000);
        result.seq_dar = DataUtils.bytesToHexString(setThenGetResponse.reads.get(i).result.dar.dataCode, 0, setThenGetResponse.reads.get(i).result.dar.dataCode.length);
        let get_result = setThenGetResponse.reads.get(i).data.result;
        switch (get_result.choice) {
            case 0: {
                let dar = {
                    dar: {}
                }
                get_result.dar.encodeAndSave(1000);
                dar.dar = DataUtils.bytesToHexString(get_result.dar.dataCode, 0, get_result.dar.dataCode.length);
                result.get_result = dar;
                break;
            }
            case 1: {
                result.get_result = parseData(get_result.data);
                break;
            }
        }
        resultArray.push(result);
    }
    frameData.code = resultCode.success;
    set_then_get_list.seq_result = resultArray;
    frameData.data = set_then_get_list;
    return JSON.stringify(frameData);
}
